#include "WidgetSpeedTrap.hpp"


WidgetSpeedTrap::WidgetSpeedTrap(const char *pRootPath, const char *pIniUIFilename, const char *pIniDriverMappingFilename, const char *pIniVehiculeClassMappingFilename)
{
	initialized = false;
	rootPath = pRootPath; 

	nbProfiles = GetPrivateProfileInt("Profiles", "count", 0, pIniUIFilename);
	for (int i = 0 ; i < nbProfiles; i++) {
		sprintf(tmp, "Profile.%d", (i+1));
		profiles[i].widgetOffsetX = (signed int)GetPrivateProfileInt(tmp, "SpeedTrap.offsetX", 0, pIniUIFilename);
		profiles[i].widgetOffsetY = (signed int)GetPrivateProfileInt(tmp, "SpeedTrap.offsetY", 0, pIniUIFilename);
		profiles[i].hide = GetPrivateProfileInt(tmp, "SpeedTrap.hide", 0, pIniUIFilename) != 0;
	}

	//std::string str = rootPath;
	GetPrivateProfileString("UI Widgets", "SpeedTrap.ini", "", tmp, 999, pIniUIFilename);
	//str.append(tmp);
	sprintf(iniUIFilename, "%s%s", rootPath, tmp);
	//iniUIFilename = str.c_str(); //pIniUIFilename; 
	available = GetPrivateProfileInt("SpeedTrap", "available", 0, iniUIFilename) != 0;

	speedStr[0] = 0;
	bestStr[0] = 0;
}

void WidgetSpeedTrap::init(const ScreenInfoV01 &info, float ratio, float ratioWidth, float ratioHeight, Sprite spriteList[]) {
	if (!available) {
		initialized = true;
		return;
	}

	for (int i = 0 ; i < nbProfiles; i++) {
		profiles[i].widgetOffsetX *= ratioWidth;
		profiles[i].widgetOffsetY *= ratioHeight;
	}

	int posX = (int)(ratioWidth * GetPrivateProfileInt("SpeedTrap", "image.left", 40, iniUIFilename));//40;
	int posY = (int)(ratioHeight * GetPrivateProfileInt("SpeedTrap", "image.top", 550, iniUIFilename));//550;

	numberOfLapBeforeDisplaying = GetPrivateProfileInt("SpeedTrap", "completed.laps.before.display", 2, iniUIFilename);
	//LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	

	bgImageData.spriteIndex = GetPrivateProfileInt("SpeedTrap", "image.id", 0, iniUIFilename) -1;
	if (bgImageData.spriteIndex > -1) {
		bgImageData.left = posX;
		bgImageData.top = posY;
		bgImageData.width = (int)(GetPrivateProfileInt("SpeedTrap", "image.width", (int)spriteList[bgImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
		bgImageData.height = (int)(GetPrivateProfileInt("SpeedTrap", "image.height", (int)spriteList[bgImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		bgImageData.colorIndex = GetPrivateProfileInt("SpeedTrap", "image.color.id", 1, iniUIFilename) -1;
	}

	speedAvailable = GetPrivateProfileInt("SpeedTrap.Speed", "available", 0, iniUIFilename) != 0;
	if (speedAvailable) {
		speedTextData.fontIndex = GetPrivateProfileInt("SpeedTrap.Speed", "text.font.id", 1, iniUIFilename) - 1;
		speedTextData.colorIndex = GetPrivateProfileInt("SpeedTrap.Speed", "text.font.color.id", 1, iniUIFilename) - 1;
		speedTextData.rect.left = (int)(posX + GetPrivateProfileInt("SpeedTrap.Speed", "text.left", 0, iniUIFilename) * ratioWidth);
		speedTextData.rect.top = (int)(posY + GetPrivateProfileInt("SpeedTrap.Speed", "text.top", 0, iniUIFilename) * ratioHeight);
		speedTextData.rect.right = (int)(posX + GetPrivateProfileInt("SpeedTrap.Speed", "text.right", 100, iniUIFilename) * ratioWidth);
		speedTextData.rect.bottom = (int)(posY + GetPrivateProfileInt("SpeedTrap.Speed", "text.bottom", 100, iniUIFilename) * ratioHeight);
		speedTextData.alignment = GetPrivateProfileInt("SpeedTrap.Speed", "text.alignment", 1, iniUIFilename);
	}

	bestSpeedAvailable = GetPrivateProfileInt("SpeedTrap.Best", "available", 0, iniUIFilename) != 0;
	if (bestSpeedAvailable) {
		bestTextData.fontIndex = GetPrivateProfileInt("SpeedTrap.Best", "text.font.id", 1, iniUIFilename) - 1;
		bestTextData.colorIndex = GetPrivateProfileInt("SpeedTrap.Best", "text.font.color.id", 1, iniUIFilename) - 1;
		bestTextData.rect.left = (int)(posX + GetPrivateProfileInt("SpeedTrap.Best", "text.left", 0, iniUIFilename) * ratioWidth);
		bestTextData.rect.top = (int)(posY + GetPrivateProfileInt("SpeedTrap.Best", "text.top", 0, iniUIFilename) * ratioHeight);
		bestTextData.rect.right = (int)(posX + GetPrivateProfileInt("SpeedTrap.Best", "text.right", 100, iniUIFilename) * ratioWidth);
		bestTextData.rect.bottom = (int)(posY + GetPrivateProfileInt("SpeedTrap.Best", "text.bottom", 100, iniUIFilename) * ratioHeight);
		bestTextData.alignment = GetPrivateProfileInt("SpeedTrap.Best", "text.alignment", 1, iniUIFilename);
	}
}

void WidgetSpeedTrap::updateData(const VehicleScoringInfoV01 &vinfo, DriverInfo & di, bool isViewedDi, TimingInfo & timingInCategory, double mET) {
	if (!available) return;

	double speed = sqrt(vinfo.mLocalVel.x * vinfo.mLocalVel.x  + vinfo.mLocalVel.y * vinfo.mLocalVel.y + vinfo.mLocalVel.z * vinfo.mLocalVel.z);
	if (speed < 0 || speed > 500) return; // avoid memory override speedStr/bestStr[11]
	if (speed > timingInCategory.bestSpeedTrap) {
		timingInCategory.bestSpeedTrap = speed;
		timingInCategory.bestSpeedLapDistance = vinfo.mLapDist;
	}

	if (!vinfo.mInPits && vinfo.mLapDist > timingInCategory.bestSpeedLapDistance - 300 && vinfo.mLapDist < timingInCategory.bestSpeedLapDistance + 200) {
		if ( di.lastSpeedAtSpeedTrap > speed) {
			if (di.speedAtSpeedTrap < di.lastSpeedAtSpeedTrap) {
				di.speedAtSpeedTrap = di.lastSpeedAtSpeedTrap;
				di.speedTrapCanBeDisplayedET = mET;
				di.speedTrapCanBeDisplayed = true;
			}
		} else {
			di.lastSpeedAtSpeedTrap = speed;
		}
	} else {
		di.lastSpeedAtSpeedTrap = 0;
		di.speedAtSpeedTrap = 0;
		di.speedTrapCanBeDisplayed = false;
	}

	if (isViewedDi && di.speedTrapCanBeDisplayed) {
		sprintf(speedStr, "%0.1f km/h", di.speedAtSpeedTrap * 3.6);
		sprintf(bestStr, "%0.1f km/h", timingInCategory.bestSpeedTrap * 3.6);
	}

}




void WidgetSpeedTrap::render(int profileIndex, const ScreenInfoV01 &info, DriverInfo & viewedDi, const LPD3DXFONT *fontList, const D3DCOLOR *colorList, Sprite spriteList[], double mET) { 
	if (!available) return;
		if (gReplayIsActive) return;
	if (profiles[profileIndex].hide) return;
	if (!viewedDi.speedTrapCanBeDisplayed) return;
	if (viewedDi.lapCompleted < numberOfLapBeforeDisplaying) return;
	if (viewedDi.speedTrapCanBeDisplayedET + 4 <= mET) {
		viewedDi.speedTrapCanBeDisplayed = false;
		return;
	}
	LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	if (bgImageData.spriteIndex > -1) {
		spriteList[bgImageData.spriteIndex].setPosition(bgImageData.left + profiles[profileIndex].widgetOffsetX, bgImageData.top + profiles[profileIndex].widgetOffsetY);
		spriteList[bgImageData.spriteIndex].setSize(bgImageData.width, bgImageData.height);
		spriteList[bgImageData.spriteIndex].render(dev, colorList[bgImageData.colorIndex], 100);
	}

	if (speedAvailable) {
		tmpRect.left = speedTextData.rect.left + profiles[profileIndex].widgetOffsetX;
		tmpRect.top = speedTextData.rect.top + profiles[profileIndex].widgetOffsetY;
		tmpRect.right = speedTextData.rect.right + profiles[profileIndex].widgetOffsetX;
		tmpRect.bottom = speedTextData.rect.bottom + profiles[profileIndex].widgetOffsetY;
		fontList[speedTextData.fontIndex]->DrawTextA(NULL, speedStr, -1, &tmpRect, speedTextData.alignment, colorList[speedTextData.colorIndex]);
	}

	if (bestSpeedAvailable) {
		tmpRect.left = bestTextData.rect.left + profiles[profileIndex].widgetOffsetX;
		tmpRect.top = bestTextData.rect.top + profiles[profileIndex].widgetOffsetY;
		tmpRect.right = bestTextData.rect.right + profiles[profileIndex].widgetOffsetX;
		tmpRect.bottom = bestTextData.rect.bottom + profiles[profileIndex].widgetOffsetY;
		fontList[bestTextData.fontIndex]->DrawTextA(NULL, bestStr, -1, &tmpRect, bestTextData.alignment, colorList[bestTextData.colorIndex]);
	}
}

WidgetSpeedTrap::~WidgetSpeedTrap(void)
{
}
